package com.platform.usercenter.controller;

import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.platform.common.core.utils.DateUtils;
import com.platform.common.core.utils.poi.ExcelUtil;
import com.platform.common.core.web.controller.BaseController;
import com.platform.common.core.web.domain.AjaxResult;
import com.platform.common.core.web.page.TableDataInfo;
import com.platform.common.security.annotation.RequiresPermissions;
import com.platform.common.security.utils.SecurityUtils;
import com.platform.constants.SysUserConstants;
import com.platform.usercenter.domain.SysAppPermission;
import com.platform.usercenter.domain.SysMenuTreeDTO;
import com.platform.usercenter.domain.SysOrganizationPermission;
import com.platform.usercenter.domain.dto.SysAppSceneDTO;
import com.platform.usercenter.domain.query.SysMenuQuery;
import com.platform.usercenter.service.ISysAppPermissionService;
import com.platform.usercenter.service.ISysMenuService;
import com.platform.usercenter.service.ISysOrganizationPermissionService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.platform.common.security.annotation.RequiresPermissions;
import com.platform.usercenter.domain.SysAppScene;
import com.platform.usercenter.service.ISysAppSceneService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 应用场景管理
 *
 * @author platform
 * @date 2024-10-28
 */
@RestController
@RequestMapping("/appScene")
public class SysAppSceneController extends BaseController {
    @Resource
    private ISysAppSceneService sysAppSceneService;
    @Resource
    private ISysAppPermissionService sysAppPermissionService;

    @Resource
    private ISysMenuService sysMenuService;

    @Resource
    private ISysOrganizationPermissionService organizationPermissionService;

    /**
     * 应用场景树结构查询
     */
    @GetMapping("/listTree")
    public AjaxResult listTree(SysAppScene sysAppScene) {
        return AjaxResult.success(sysAppSceneService.listTree(sysAppScene));
    }

    /**
     * 应用场景绑定key值-新增
     */
    @RequiresPermissions("usercenter:bindAppKey:bind")
    @PostMapping("bindAppKey")
    public AjaxResult bindAppKey(@RequestBody SysAppSceneDTO dto) {
        return AjaxResult.success(sysAppSceneService.bindAppKey(dto));
    }

    /**
     * 应用场景绑定key值-根据应用id查询列表
     */
    @GetMapping("bindAppQuery/{appSceneId}")
    public AjaxResult bindAppQuery(@PathVariable("appSceneId") Long appSceneId) {
        SysAppPermission permission = new SysAppPermission();
        permission.setAppSceneId(appSceneId);
        permission.setIsDelete(SysUserConstants.N);
        return AjaxResult.success(sysAppPermissionService.selectSysAppPermissionList(permission));
    }

    /**
     * 应用场景绑定key值-根据绑定key值id查询详情
     */
    @GetMapping("bindAppInfo/{permissionId}")
    public AjaxResult bindAppInfo(@PathVariable("permissionId") Long permissionId) {
        return AjaxResult.success(sysAppPermissionService.selectSysAppPermissionByPermissionId(permissionId));
    }

    /**
     * 应用场景绑定key值-修改
     */
    @RequiresPermissions("usercenter:bindAppKey:edit")
    @PostMapping("bindAppEdit")
    public AjaxResult bindAppEdit(@RequestBody SysAppPermission sysAppPermission) {
        return AjaxResult.success(sysAppPermissionService.updateSysAppPermission(sysAppPermission));
    }

    /**
     * 应用场景绑定key值-删除
     */
    @RequiresPermissions("usercenter:bindAppKey:remove")
    @DeleteMapping("bindAppDel/{permissionIds}")
    public AjaxResult bindAppDel(@PathVariable Long permissionIds) {
        SysAppPermission permission = new SysAppPermission();
        permission.setPermissionId(permissionIds);
        permission.setIsDelete(SysUserConstants.Y);
        permission.setDeleteBy(SecurityUtils.getUsername());
        permission.setDeleteTime(DateUtils.getNowDate());
        return AjaxResult.success(sysAppPermissionService.updateSysAppPermission(permission));
    }


    /**
     * 查询应用场景管理列表
     */
    @GetMapping("/list")
    public TableDataInfo list(SysAppScene sysAppScene) {
        startPage();
        List<SysAppScene> list = sysAppSceneService.selectSysAppSceneList(sysAppScene);
        return getDataTable(list);
    }

    /**
     * 导出应用场景管理列表
     */
    @RequiresPermissions("usercenter:appScene:export")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysAppScene sysAppScene) {
        List<SysAppScene> list = sysAppSceneService.selectSysAppSceneList(sysAppScene);
        ExcelUtil<SysAppScene> util = new ExcelUtil<SysAppScene>(SysAppScene.class);
        util.exportExcel(response, list, "应用场景管理数据");
    }

    /**
     * 获取应用场景管理详细信息
     */
    @RequiresPermissions("usercenter:appScene:query")
    @GetMapping(value = "/{appSceneId}")
    public AjaxResult getInfo(@PathVariable("appSceneId") Long appSceneId) {
        return success(sysAppSceneService.selectSysAppSceneByAppId(appSceneId));
    }

    /**
     * 新增应用场景
     */
    @RequiresPermissions("usercenter:appScene:add")
    @PostMapping
    public AjaxResult add(@RequestBody SysAppScene sysAppScene) {
        return toAjax(sysAppSceneService.insertSysAppScene(sysAppScene));
    }

    /**
     * 修改应用场景管理
     */
    @RequiresPermissions("usercenter:appScene:edit")
    @PutMapping
    public AjaxResult edit(@RequestBody SysAppScene sysAppScene) {
        return toAjax(sysAppSceneService.updateSysAppScene(sysAppScene));
    }

    /**
     * 删除应用场景管理
     */
    @RequiresPermissions("usercenter:appScene:remove")
	@DeleteMapping("/{appSceneIds}")
    public AjaxResult remove(@PathVariable Long[] appSceneIds)
    {
        // 校验是否应用绑定了菜单，组织，如果有绑定则不允许删除
        if(ObjectUtil.isNotEmpty(appSceneIds)){
            for (Long id : appSceneIds){
                SysMenuQuery query = new SysMenuQuery();
                query.setAppSceneId(id);
                List<SysMenuTreeDTO>  menuDTOS=sysMenuService.selectMenuListCheckApp(query);
                if(CollectionUtil.isNotEmpty(menuDTOS)){
                    return AjaxResult.error(id+"已关联菜单，不可删除");
                }
                SysOrganizationPermission orgQUery = new SysOrganizationPermission();
                orgQUery.setAppSceneId(id);
                orgQUery.setIsDelete(SysUserConstants.N);
                List<SysOrganizationPermission> permissions=organizationPermissionService.selectSysOrganizationPermissionList(orgQUery);
                if(CollectionUtil.isNotEmpty(permissions)){
                    return AjaxResult.error(id+"已关联组织，不可删除");
                }
            }
        }
        return toAjax(sysAppSceneService.deleteSysAppSceneByAppIds(appSceneIds));
    }
}
